package com.yunsoft.notice.service.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apistd.uni.Uni;
import com.apistd.uni.UniException;
import com.apistd.uni.UniResponse;
import com.apistd.uni.sms.UniMessage;
import com.apistd.uni.sms.UniSMS;
import com.fld.central.common.model.code.BaseCode;
import com.fld.central.common.model.exception.SpiException;
import com.fld.platform.http.HttpClientMgrBean;
import com.fld.platform.http.HttpResult;
import com.fld.platform.http.ResultCode;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UniSmsManager implements Serializable {


    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(UniSmsManager.class);

    private static final String APP_ID = "75163194649364831463";
    private static final String APP_KEY = "5bl96v6k2xmkoo811xjng6w2g6qg1e";
    /**验证码通知类短信*/
    private static final String SINGLE = "http://inner-sp-trade.1sapp.com/sms/single";
    /**营销短信*/
    private static final String BATCH = "http://inner-sp-marketing.1sapp.com/sms/batch";
    public static String ACCESS_KEY_ID = "your access key id";
    private static String ACCESS_KEY_SECRET = "your access key secret";

    private HttpClientMgrBean httpClient = HttpClientMgrBean.createInstance();

    private static final Map<String,String> RESULT_CODE = new HashMap<String,String>();
    static {
        RESULT_CODE.put("0", "成功");
        RESULT_CODE.put("1", "系统错误");
        RESULT_CODE.put("2", "http失败");
        RESULT_CODE.put("3", "系统错误");
        RESULT_CODE.put("7", "参数错误");
        RESULT_CODE.put("10", "黑名单中");
        RESULT_CODE.put("11", "找不到通道商");
        RESULT_CODE.put("12", "验签错误");
        RESULT_CODE.put("14", "通道商同步返回失败");
    }



    public static void main(String[] args) {
//		Map<String,String> params = new HashMap<String,String>();
//		params.put("month", "12");
//		params.put("day", "10");
//		params.put("week", "三");
//		params.put("amount", "12.35");
//
//		SMSTemplateCode templateCode = SMSTemplateCode.SMS_179216033;
//		String content = templateCode.getContent();
//		Set<String> keys = params.keySet();
//		for (String key : keys) {
//			content = content.replaceAll("\\$\\{"+key+"\\}", params.get(key));
//		}
//		System.out.println(content);

//        SMSMgrBean bean = new SMSMgrBean();
//		Map<String,String> params = new HashMap<String,String>();
//		params.put("code", "123654");
//		bean.publish(null, SMSTemplateCode.SMS_95550034, params, new String[] {"13554793093"});

//        bean.publish("13554793093", "生产短信测试"+System.currentTimeMillis());
//		List<String> phoneList = new ArrayList<String>();
//		phoneList.add("13554793093");
//		bean.publish("营销短信测试", phoneList);
    }


    /**
     * 生成前面
     * @param timestamp
     * @return
     */
    private String createSign(int timestamp) {
        return DigestUtils.md5Hex(APP_ID + APP_KEY + timestamp);
    }

    /**
     * 发送验证码或通知类短信，签名统一“库盒”
     * @param phone	-	接收手机号
     * @param code	-	短信内容
     * @return
     */
    public String publish(String phone,String code) throws SpiException {
        // 初始化
        Uni.init(ACCESS_KEY_ID, ACCESS_KEY_SECRET); // 若使用简易验签模式仅传入第一个参数即可

        // 设置自定义参数 (变量短信)
        Map<String, String> templateData = new HashMap<String, String>();
        templateData.put("code", code);

        // 构建信息
        UniMessage message = UniSMS.buildMessage()
            .setTo(phone)
            .setSignature("UniSMS")
            .setTemplateId("login_tmpl")
            .setTemplateData(templateData);

        // 发送短信
        UniResponse res = null;
        try {
            res = message.send();
            System.out.println(res);
        } catch (UniException e) {
            System.out.println("Error: " + e);
            System.out.println("RequestId: " + e.requestId);
        throw new SpiException(BaseCode.ERROR,String.format("发送失败，HTTP Status：%s，返回内容：%s",res,""));

        }
        return "成功";
    }


}
